package com.mindcraft.shop.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.shop.dto.CategoryTreeDto;
import com.mindcraft.shop.mapper.CategoryMapper;
import com.mindcraft.shop.mapper.SkuMapper;
import com.mindcraft.shop.po.Category;
import com.mindcraft.shop.po.SkuInfo;
import com.mindcraft.shop.service.CategoryService;
import com.mindcraft.shop.service.SkuInfoService;
import com.mindcraft.shop.vo.SkuInfoQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mr.M
 * @version 1.0
 * @description 微信扫码认证
 * @date 2022/9/29 12:12
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public List<CategoryTreeDto> queryTreeNodes(Long id) {
        List<CategoryTreeDto> categoryTreeDtos = categoryMapper.selectTreeNodes(id);
        //将list转map,以备使用,排除根节点
        Map<Long, CategoryTreeDto> mapTemp =
                categoryTreeDtos.stream()
                        .filter(item -> !id.equals(item.getId()))
                        .collect(Collectors.toMap(key -> key.getId(), value -> value, (key1, key2) -> key2));
        //最终返回的list
        List<CategoryTreeDto> categoryTreeDtoList = new ArrayList<>();
        //依次遍历每个元素,排除根节点
        categoryTreeDtos.stream().filter(item -> !id.equals(item.getId())).forEach(item -> {
            if (item.getParentId().equals(id)) {
                categoryTreeDtoList.add(item);
            }
            //找到当前节点的父节点
            CategoryTreeDto CategoryTreeDto = mapTemp.get(item.getParentId());
            if (CategoryTreeDto != null) {
                if (CategoryTreeDto.getChildrenTreeNodes() == null) {
                    CategoryTreeDto.setChildrenTreeNodes(new ArrayList<CategoryTreeDto>());
                }
                //下边开始往ChildrenTreeNodes属性中放子节点
                CategoryTreeDto.getChildrenTreeNodes().add(item);
            }
        });
        return categoryTreeDtoList;
    }

    @Override
    public MindCraftResult getCategoryAllTree() {
        // 获取所有课程数据，避免循环查表消耗大量资源
        List<Category> list = categoryMapper.selectList(null);
        // 子分类 ID 数组
        List<Long> childrenIds = new ArrayList<>();

        List<Category> collect = list.stream()
                //过滤出已经作为子分类的数据
                .filter(item -> !childrenIds.contains(item.getId()))
                .map(menu -> {
                    menu.setCategoryList(getChildren(menu, list, childrenIds));

                    return menu;
                }).collect(Collectors.toList());
        return MindCraftResult.success(collect);
    }

    private List<Category> getChildren(Category root, List<Category> list, List<Long> childrenIds) {
        List<Category> children = list.stream()
                // 过滤出当前节点的子分类，且排除已经作为子分类的数据
                .filter(item -> item.getParentId().equals(root.getId()) && !childrenIds.contains(item.getId()))
                .map(menu -> {
                    menu.setCategoryList(getChildren(menu, list, childrenIds));
                    return menu;
                }).collect(Collectors.toList());
        // 将当前节点的子分类 ID 添加到数组中
        childrenIds.addAll(children.stream().map(Category::getId).collect(Collectors.toList()));
        return children;
    }


}